/*
 * Copyright (c) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * introduction:
 *  语音模块串口驱动
 */

#include <stdio.h>
#include <unistd.h>
#include "ohos_init.h"
#include "cmsis_os2.h"

#include "hi_io.h"
#include "hi_gpio.h"
#include "hi_uart.h"
#include "iot_uart.h"
#include "speaker.h"
#define UART_TIMEOUT 10

int Speaker_Init()
{
    hi_io_set_func(SPEAKER_RX_12, HI_IO_FUNC_GPIO_12_UART2_RXD);
    hi_io_set_func(SPEAKER_TX_11, HI_IO_FUNC_GPIO_11_UART2_TXD);

    hi_uart_attribute my_uart2;
    my_uart2.baud_rate = 115200;
    my_uart2.data_bits = 8;
    my_uart2.parity = HI_UART_PARITY_NONE;
    my_uart2.stop_bits = 1;

    return hi_uart_init(HI_UART_IDX_2, &my_uart2, NULL);
}

int Speaker_read(char *get_data, int data_len)
{
    if (get_data == NULL)
    {
        return -1;
    }
    int ret;
    ret = hi_uart_read_timeout(HI_UART_IDX_2, get_data, data_len, UART_TIMEOUT);
    return ret;
}

int Speaker_write(char *get_data, int data_len)
{
    if (get_data == NULL)
    {
        return -1;
    }
    int ret;
    ret = hi_uart_write_immediately(HI_UART_IDX_2, get_data, data_len);
    return ret;
}
